package com.cn.springcloudshop.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cn.springcloudshop.common.AjaxRes;
import com.cn.springcloudshop.common.Const;
import com.cn.springcloudshop.model.YmShopCat;
import com.cn.springcloudshop.model.YmShopCatExample;
import com.cn.springcloudshop.model.YmShopCatPOJO;
import com.cn.springcloudshop.service.inter.CatService;
import org.apache.commons.lang3.StringUtils;
import org.nutz.json.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;

/**
 * @Author: wuyanzu
 * @Date: 2018/8/13
 */
@RequestMapping("/cat/")
@RestController
public class CatController {

    @Autowired
    private CatService catService;

    /**
    *
    * 查询顶层类目信息
    * @author wuyanzu
    * @date 2018/8/15 15:48
    * @param [ajaxRes]
    * @return com.cn.springcloudshop.common.AjaxRes
    */
    @RequestMapping("getCat")
    public AjaxRes getTopCat(AjaxRes ajaxRes) {
        YmShopCatExample ymShopCatExample = new YmShopCatExample();
        ymShopCatExample.setOrderByClause("sort_order asc");
        ymShopCatExample.createCriteria().andParentIdEqualTo(0L);
        List<YmShopCat> list = catService.selectByExample(ymShopCatExample);
        if (list != null && list.size() > 0) {
            ajaxRes.setSucceed(list);
        } else {
            ajaxRes.setSucceedMsg("暂无类目信息");
        }
        return ajaxRes;
    }

    /**
    *
    * 批量修改商品类目状态
    * @author wuyanzu
    * @date 2018/8/14 14:19
    * @param [ajaxRes, ids, status]
    * @return com.cn.springcloudshop.common.AjaxRes
    */
    @RequestMapping("del")
    public AjaxRes delCatByPK(AjaxRes ajaxRes,@RequestParam("ids[]") Long [] ids,Integer status) {
        if ((ids == null || ids.length < 1) && status == null) {
            ajaxRes.setFailMsg(Const.PARAMETER_NULL);
            return ajaxRes;
        }
        List<Long> list = java.util.Arrays.asList(ids);;
        if (catService.deleteBatchByPK(list, status) != 0) {
            ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
        } else {
            ajaxRes.setFailMsg(Const.UPDATE_FAIL);
        }
        return ajaxRes;
    }

    /**
    *
    * 添加商品类目
    * @author wuyanzu
    * @date 2018/8/14 14:19
    * @param [ajaxRes, cat]
    * @return com.cn.springcloudshop.common.AjaxRes
    */
    @RequestMapping("add")
    public AjaxRes createCat(AjaxRes ajaxRes,String cat) {
        if (!StringUtils.isNotEmpty(cat)) {
            ajaxRes.setFailMsg(Const.PARAMETER_NULL);
            return ajaxRes;
        }
        YmShopCat ymShopCat = JSONObject.parseObject(cat, YmShopCat.class);
        if (catService.insertSelective(ymShopCat) == 1) {
            ajaxRes.setSucceedMsg(Const.SAVE_SUCCEED);
        } else {
            ajaxRes.setFailMsg(Const.SAVE_FAIL);
        }
        return ajaxRes;
    }

    /**
    *
    * 获取父节点下所有子节点
    * @author wuyanzu
    * @date 2018/8/14 14:20
    * @param [ajaxRes, parentId]
    * @return com.cn.springcloudshop.common.AjaxRes
    */
    @RequestMapping("get")
    public AjaxRes selectCat(AjaxRes ajaxRes,Long parentId) {
        if (parentId == null) {
            ajaxRes.setFailMsg(Const.PARAMETER_NULL);
        } else {
            List<YmShopCatPOJO> list = catService.findCatByParentId(parentId);
            if (list != null && list.size() > 0) {
                JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(list));
                String jsonString = jsonArray.toString().replace("\"children\":[],", "");
                ajaxRes.setSucceed(JSONArray.parseArray(jsonString));
                return ajaxRes;
            } else {
                ajaxRes.setSucceed(null,"暂无类目信息");
            }
        }
        return ajaxRes;
    }

    /**
    *
    * 跟新类目信息
    * @author wuyanzu
    * @date 2018/8/14 14:20
    * @param [ajaxRes, cat]
    * @return com.cn.springcloudshop.common.AjaxRes
    */
    @RequestMapping("update")
    public AjaxRes updateCatByPK(AjaxRes ajaxRes, String cat) {
        if (StringUtils.isNotEmpty(cat)) {
            YmShopCat ymShopCat = JSONObject.parseObject(cat, YmShopCat.class);
            if (ymShopCat.getParentId() == ymShopCat.getId()) {
                ajaxRes.setFailMsg("不能以自己作为父类");
            }else {
                if (catService.updateByPrimaryKeySelective(ymShopCat) == 1) {
                    ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
                } else {
                    ajaxRes.setFailMsg(Const.UPDATE_FAIL);
                }
            }
        } else {
            ajaxRes.setFailMsg(Const.PARAMETER_NULL);
        }
        return ajaxRes;
    }

    /**
    *
    * 条件查询
    * @author wuyanzu
    * @date 2018/8/14 14:20
    * @param [ajaxRes, pageNum, pageSize]
    * @return com.cn.springcloudshop.common.AjaxRes
    */
    @RequestMapping("conditionalQuery")
    public AjaxRes conditionalQuery(AjaxRes ajaxRes,@RequestParam(required = false)Integer pageNum,
                                    @RequestParam(required = false)Integer pageSize) {
        YmShopCatExample ymShopCatExample = new YmShopCatExample();
        YmShopCatExample.Criteria criteria = ymShopCatExample.createCriteria();
        int size = catService.countByExample(ymShopCatExample);
        int totalPage = 1;
        if (size % pageSize == 0) {
            totalPage = size / pageSize;
        } else {
            totalPage = size / pageSize + 1;
        }
        if (pageNum != null) {
            if (size > pageSize) {
                ymShopCatExample.setLimit((pageNum - 1) * pageSize);
            } else {
                ymShopCatExample.setLimit(0);
            }
            ymShopCatExample.setOffset(pageSize);
        }
        List<YmShopCat> list = catService.selectByExample(ymShopCatExample);
        for (YmShopCat ymShopCat : list) {
            if (ymShopCat.getParentId() == 0) {
                ymShopCat.setParentName("/");
            } else {
                ymShopCat.setParentName(catService.selectByPrimaryKey(ymShopCat.getParentId()).getText());
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("totalPage", totalPage);
        map.put("totalRecord", size);
        if (list != null && list.size() > 0) {
            map.put("list", list);
            ajaxRes.setSucceed(map, Const.DATA_SUCCEED);
        } else {
            map.put("list", null);
            ajaxRes.setSucceed(map,Const.DATA_SUCCEED);
        }
        return ajaxRes;
    }
}

